import * as mobx from 'mobx';
import { observable, action, computed, asMap } from 'mobx';
import * as API_REQ from '../apis/req';
import * as _ from '../util/public/common';
import base from '../util/base';

export class StatusStore{
    baseParam = {};
    @observable cardid = "";
    @observable counts = {};
    @observable dataMap = observable.map ? observable.map({}) : asMap({});
    paramMap = observable.map ? observable.map({}) : asMap({});
    @observable hideRowKeys = [];
    @observable nodeOptions = [{key: "-1",selected: false,showname: ""}];//节点选项
    @observable selectedNode = "-1";//选择的节点
    @observable selectedOperator = "";//选择的操作者
    @observable isHideAll = false;

    constructor(){
        this.switchCard = this.switchCard;
        this.loadData = this.loadData;
        this.loadCounts = this.loadCounts;
        this.controlHideRow = this.controlHideRow;
        this.controlHideAllRow = this.controlHideAllRow;
        this.setSelectedNode = this.setSelectedNode;
        this.setSelectedOperator = this.setSelectedOperator;
        this.clearnSearchStatus = this.clearnSearchStatus;
    }

    @action
    setBaseParam = (param={}) => {
        this.baseParam = param;
    }

    @action
    setCounts = (counts) => {
        this.counts = counts;
    }

    @action
    setSelectedNode = (id)  => {
        this.selectedNode = id;
    }

    @action
    setSelectedOperator = (id) =>  {
        this.selectedOperator = id;
    }

    @action
    appendDataMap = (data) => {
        this.dataMap.set(this.cardid, data);
    }

    @action('切换卡片类型')
    switchCard = (cardid) => {
        if(cardid.indexOf("_") > -1) {//有搜索条件的情况
            cardid = cardid.substring(0, cardid.indexOf("_"));
        }
        if(this.getHasAdSearch()) {
            cardid = cardid + "_" +  this.selectedNode + "_" + this.selectedOperator;
        }
        this.cardid = cardid;
        if(!this.dataMap.has(cardid))
            this.loadData(true);
    }

    @action('加载流程状态数据')
    loadData = (isfirst) => {
		let params = {...this.baseParam, ...{desremark:this.cardid, isfirst:isfirst, pageSize:50, selectedNode : this.selectedNode, selectedOperator : this.selectedOperator}};
		if(!isfirst)
            params["parameter"] = JSON.stringify(this.paramMap.get(this.cardid));
        base.globalStore.setLoading(true);
        const isAdSearch = this.getHasAdSearch();
		API_REQ.getWfStatus(params).then(data => {
            base.globalStore.setLoading(false);
            const { datas,parameter } = data;
            let newData = this.dataMap.has(this.cardid) ? _.cloneDeep(mobx.toJS(this.dataMap.get(this.cardid))) : {};
            !_.isEmpty(datas) && _.forIn(datas, (nodeData,key) =>{
                if(key in newData){     //同节点追加数据
                    newData[key].list = _.concat(newData[key].list || [], nodeData.list || [])
                }else{
                    newData[key] = nodeData;
                }
            });
            if(isAdSearch && isfirst) {
                if(!this.counts[this.cardid + "count"]) {
                    this.counts[this.cardid + "count"] = data.count;
                }
            }
            this.appendDataMap(newData);
            this.paramMap.set(this.cardid, parameter);
        });
    }

    @action('加载计数信息')
    loadCounts = () => {
        API_REQ.getWfStatusCount(this.baseParam).then(data => {
            this.setCounts(data);
            this.nodeOptions = data.selectNodeOptions;
        });
    }

    @action('控制显示/隐藏的行节点')
    controlHideRow = (key) => {
        const index = _.indexOf(this.hideRowKeys, key);
        if(index === -1)
            this.hideRowKeys.push(key);
        else
            this.hideRowKeys.splice(index, 1);      //删除一项
    }

    @action('控制显示/隐藏全部的行节点')
    controlHideAllRow = (size) => {
        this.isHideAll = !this.isHideAll;
        this.hideRowKeys = [];
        if(this.isHideAll) {
            for(let i=0; i<size; i++) {
                this.hideRowKeys.push(`rowDataHead_${i+1}`);
            }
        }
    }

    getHasAdSearch = () =>  {
        return this.selectedOperator != "" || this.selectedNode != "-1";
    }

    @action
    clearnSearchStatus = ()  => {
        this.selectedNode = "-1";
        this.selectedOperator = "";
        this.isHideAll = false;
        this.hideRowKeys = [];
    }
}